package com.thread.exe;

import java.util.concurrent.Exchanger;

public class TestExchanger {
	public static void main(String[] args) {
		Exchanger<Integer> ex = new Exchanger<Integer>();
		Thread t1 = new Thread() {
			public void run() {
				int myNum = 5;
				try {
					int yourNum = ex.exchange(myNum);
					System.out.println("t1 交换前" + myNum + "交换后" + yourNum);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			};
		};
		Thread t2 = new Thread() {
			public void run() {
				int myNum = 3;
				try {
					int yourNum = ex.exchange(myNum);
					System.out.println("t2 交换前" + myNum + "交换后" + yourNum);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			};
		};
		
		t1.start();
		t2.start();
	}
}/**
Output:
t2 交换前3交换后5
t1 交换前5交换后3
*/
